﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Sapiens.at.SharePoint.Extensions
{
    /// <summary>
    /// Extension methods for <see cref="SPListItem"/>.
    /// </summary>
    public static class ListItemEx
    {
        //public static T Get<T>(this SPListItem listItem, string fieldName)
        //{
        //    var value = listItem[fieldName];
        //    if (value == null) { return default(T); }
        //    if (value is T) { return (T)value; }
        //    return (T)Convert.ChangeType(value, typeof(T));
        //}

        //public static IEnumerable<object> GetAll(this SPListItem listItem)
        //{
        //    var values = listItem.Fields.Cast<SPField>().Select(f => listItem[f.Id]);
        //    return values;
        //}

        //public static IEntity GetEntity(this SPListItem listItem)
        //{
        //    System.Reflection.Assembly callingAssembly = System.Reflection.Assembly.GetCallingAssembly();
        //    if (callingAssembly == System.Reflection.Assembly.GetAssembly(typeof(SharePointExtensions)))
        //        throw new InvalidOperationException(string.Format(Resources.ErrorDoNotInvokeMethod, System.Reflection.MethodInfo.GetCurrentMethod().Name, callingAssembly.FullName));
        //    return Entity.GetEntity(listItem, callingAssembly);
        //}

        //public static TEntity GetEntity<TEntity>(this SPListItem listItem)
        //    where TEntity : IEntity, new()
        //{
        //    return Entity.GetEntity<TEntity>(listItem);
        //}

        //public static TField GetFieldValue<TField>(this SPListItem listItem, string fieldName)
        //{
        //    return Conversion.SafeCast<TField>(listItem[fieldName]);
        //}

        /// <summary>
        /// Returns true if the listitem exists, has a file and is checked out; otherwise false.
        /// </summary>
        /// <param name="listItem"></param>
        /// <returns></returns>
        public static bool IsCheckedOut(this SPListItem listItem)
        {
            return listItem != null && listItem.File != null && listItem.File.CheckedOutByUser != null;
        }

        public static SPListItem GetListItem(this SPFieldLookupValue lookupValue, SPList list)
        {
            try
            {
                return list.GetItemById(lookupValue.LookupId);
            }
            catch { return null; }
        }
    }
}
